<template>
  <view class="group-buying-page">
    <!-- 搜索栏 -->
    <view class="search-section">
      <u-search 
        v-model="searchKeyword" 
        placeholder="搜索商品名称"
        :show-action="false"
        bg-color="#f5f5f5"
        @search="handleSearch"
        @clear="handleClear"
      ></u-search>
    </view>

    <!-- 分类筛选 -->
    <view class="filter-section">
      <scroll-view scroll-x enable-flex class="filter-scroll">
        <view class="filter-list">
          <view 
            v-for="(item, index) in filterList" 
            :key="index"
            class="filter-item"
            :class="{ active: currentFilter === item.value }"
            @click="handleFilterChange(item.value)"
          >
            <text class="filter-text">{{ item.label }}</text>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- 团购商品列表 -->
    <view class="product-list">
      <view 
        v-for="(item, index) in productList" 
        :key="item.id"
        class="product-item"
        @click="handleItemClick(item)"
      >
        <!-- 商品图片 -->
        <view class="product-image">
          <image :src="item.image" mode="aspectFill" class="image"></image>
          <view v-if="item.isHot" class="hot-badge">
            <text class="hot-text">热销</text>
          </view>
          <view v-if="item.discount" class="discount-badge">
            <text class="discount-text">{{ item.discount }}折</text>
          </view>
        </view>

        <!-- 商品内容 -->
        <view class="product-content">
          <view class="content-header">
            <text class="product-title">{{ item.title }}</text>
            <view class="product-category">
              <u-tag 
                :text="item.category" 
                type="error"
                size="mini"
              ></u-tag>
            </view>
          </view>
          
          <view class="content-body">
            <text class="product-description">{{ item.description }}</text>
          </view>
          
          <view class="content-footer">
            <view class="price-info">
              <text class="current-price">¥{{ item.currentPrice }}</text>
              <text v-if="item.originalPrice" class="original-price">¥{{ item.originalPrice }}</text>
            </view>
            
            <view class="progress-info">
              <view class="progress-bar">
                <view class="progress-fill" :style="{ width: item.progress + '%' }"></view>
              </view>
              <text class="progress-text">{{ item.soldCount }}/{{ item.targetCount }}人</text>
            </view>
          </view>
        </view>

        <!-- 团购按钮 -->
        <view class="group-buy-btn" :class="getButtonClass(item.status)" @click.stop="handleGroupBuy(item)">
          <text class="btn-text">{{ getButtonText(item.status) }}</text>
        </view>
      </view>
    </view>

    <!-- 空状态 -->
    <view v-if="productList.length === 0" class="empty-state">
      <u-icon name="shopping-cart" size="48" color="#ccc"></u-icon>
      <text class="empty-text">暂无团购商品</text>
    </view>

    <!-- 加载更多 -->
    <view v-if="hasMore" class="load-more">
      <u-loading-icon v-if="loading" mode="circle"></u-loading-icon>
      <text v-else class="load-text" @click="loadMore">点击加载更多</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

// 搜索关键词
const searchKeyword = ref('')

// 当前筛选条件
const currentFilter = ref('all')

// 筛选列表
const filterList = reactive([
  { label: '全部', value: 'all' },
  { label: '进行中', value: 'ongoing' },
  { label: '即将开始', value: 'upcoming' },
  { label: '已结束', value: 'ended' },
  { label: '热销', value: 'hot' }
])

// 商品列表数据
const productList = ref([
  {
    id: 1,
    title: '新鲜有机蔬菜套餐',
    description: '包含西红柿、黄瓜、茄子、青椒等时令蔬菜，新鲜采摘，营养丰富...',
    category: '生鲜食品',
    status: 1,
    statusText: '团购中',
    currentPrice: 29.9,
    originalPrice: 39.9,
    soldCount: 45,
    targetCount: 100,
    progress: 45,
    image: 'https://example.com/images/vegetables.jpg',
    isHot: true,
    discount: 7.5
  },
  {
    id: 2,
    title: '优质大米 5kg装',
    description: '东北优质大米，粒粒饱满，口感香甜，家庭必备主食...',
    category: '粮油副食',
    status: 1,
    statusText: '团购中',
    currentPrice: 35.8,
    originalPrice: 45.8,
    soldCount: 78,
    targetCount: 80,
    progress: 97.5,
    image: 'https://example.com/images/rice.jpg',
    isHot: false,
    discount: 7.8
  },
  {
    id: 3,
    title: '新鲜水果礼盒',
    description: '包含苹果、橙子、香蕉、葡萄等新鲜水果，营养丰富，口感佳...',
    category: '生鲜食品',
    status: 2,
    statusText: '即将开始',
    currentPrice: 58.8,
    originalPrice: 78.8,
    soldCount: 0,
    targetCount: 50,
    progress: 0,
    image: 'https://example.com/images/fruits.jpg',
    isHot: false,
    discount: 7.5
  },
  {
    id: 4,
    title: '清洁用品套装',
    description: '包含洗衣液、洗洁精、洗手液等清洁用品，家庭清洁必备...',
    category: '日用品',
    status: 1,
    statusText: '团购中',
    currentPrice: 42.9,
    originalPrice: 52.9,
    soldCount: 32,
    targetCount: 60,
    progress: 53.3,
    image: 'https://example.com/images/cleaning.jpg',
    isHot: false,
    discount: 8.1
  },
  {
    id: 5,
    title: '儿童玩具套装',
    description: '益智玩具套装，包含积木、拼图等，开发儿童智力，寓教于乐...',
    category: '母婴用品',
    status: 3,
    statusText: '已结束',
    currentPrice: 89.9,
    originalPrice: 119.9,
    soldCount: 50,
    targetCount: 50,
    progress: 100,
    image: 'https://example.com/images/toys.jpg',
    isHot: true,
    discount: 7.5
  }
])

// 加载状态
const loading = ref(false)
const hasMore = ref(true)

// 获取按钮样式类
const getButtonClass = (status: number) => {
  switch (status) {
    case 1: return 'btn-group-buy'
    case 2: return 'btn-upcoming'
    case 3: return 'btn-ended'
    default: return 'btn-default'
  }
}

// 获取按钮文本
const getButtonText = (status: number) => {
  switch (status) {
    case 1: return '立即参团'
    case 2: return '即将开始'
    case 3: return '已结束'
    default: return '查看详情'
  }
}

// 搜索处理
const handleSearch = (value: string) => {
  console.log('搜索商品:', value)
  // 这里实现搜索逻辑
}

// 清空搜索
const handleClear = () => {
  searchKeyword.value = ''
  console.log('清空搜索')
}

// 筛选条件改变
const handleFilterChange = (value: string) => {
  currentFilter.value = value
  console.log('筛选条件改变:', value)
  // 这里实现筛选逻辑
}

// 点击商品项
const handleItemClick = (item: any) => {
  console.log('点击商品:', item)
  // 跳转到商品详情页面
  uni.navigateTo({
    url: `/pages/community-group-buying/detail?id=${item.id}`
  })
}

// 团购处理
const handleGroupBuy = (item: any) => {
  if (item.status === 1) {
    // 跳转到团购页面
    uni.navigateTo({
      url: `/pages/community-group-buying/buy?id=${item.id}`
    })
  } else if (item.status === 2) {
    uni.showToast({
      title: '团购尚未开始',
      icon: 'none'
    })
  } else if (item.status === 3) {
    uni.showToast({
      title: '团购已结束',
      icon: 'none'
    })
  }
}

// 加载更多
const loadMore = () => {
  if (loading.value) return
  
  loading.value = true
  setTimeout(() => {
    // 模拟加载更多数据
    loading.value = false
    hasMore.value = false
  }, 1000)
}

// 页面加载
onMounted(() => {
  console.log('社区团购页面加载完成')
})
</script>

<style lang="scss" scoped>
.group-buying-page {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 0 0 20rpx 0;
}

.page-header {
  background: linear-gradient(135deg, #ff4d4f 0%, #ff7875 100%);
  padding: 20rpx 30rpx 30rpx;
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .header-title {
      .title-text {
        display: block;
        font-size: 36rpx;
        font-weight: 600;
        color: #fff;
        margin-bottom: 8rpx;
      }
      
      .title-subtitle {
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.8);
      }
    }
    
    .header-icon {
      width: 80rpx;
      height: 80rpx;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.search-section {
  padding: 24rpx 32rpx;
  background-color: #fff;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.filter-section {
  background-color: #fff;
  border-bottom: 1rpx solid #f0f0f0;
  
  .filter-scroll {
    white-space: nowrap;
    padding: 0 24rpx 24rpx 24rpx;
    
    .filter-list {
      display: flex;
      padding: 24rpx 0 0 0;
      width: max-content;
      
      .filter-item {
        flex-shrink: 0;
        padding: 16rpx 20rpx;
        margin-right: 12rpx;
        border-radius: 30rpx;
        background-color: #f5f5f5;
        transition: all 0.3s ease;
        
        &.active {
          background-color: #ff4d4f;
          
          .filter-text {
            color: #fff;
          }
        }
        
        .filter-text {
          font-size: 24rpx;
          color: #666;
          white-space: nowrap;
        }
      }
    }
  }
}

.product-list {
  padding: 20rpx 30rpx;
  
  .product-item {
    background-color: #fff;
    border-radius: 16rpx;
    margin-bottom: 20rpx;
    padding: 30rpx;
    position: relative;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
    
    &:active {
      transform: scale(0.98);
    }
    
    .product-image {
      position: relative;
      width: 100%;
      height: 300rpx;
      border-radius: 12rpx;
      overflow: hidden;
      margin-bottom: 20rpx;
      
      .image {
        width: 100%;
        height: 100%;
      }
      
      .hot-badge {
        position: absolute;
        top: 12rpx;
        left: 12rpx;
        background: linear-gradient(45deg, #ff6b6b, #ff8e8e);
        color: #fff;
        padding: 6rpx 12rpx;
        border-radius: 12rpx;
        font-size: 20rpx;
      }
      
      .discount-badge {
        position: absolute;
        top: 12rpx;
        right: 12rpx;
        background: linear-gradient(45deg, #ff4d4f, #ff7875);
        color: #fff;
        padding: 6rpx 12rpx;
        border-radius: 12rpx;
        font-size: 20rpx;
      }
    }
    
    .product-content {
      .content-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 16rpx;
        
        .product-title {
          flex: 1;
          font-size: 32rpx;
          font-weight: 600;
          color: #333;
          line-height: 1.4;
          margin-right: 20rpx;
        }
        
        .product-category {
          flex-shrink: 0;
        }
      }
      
      .content-body {
        margin-bottom: 20rpx;
        
        .product-description {
          font-size: 28rpx;
          color: #666;
          line-height: 1.5;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
      
      .content-footer {
        .price-info {
          display: flex;
          align-items: center;
          gap: 12rpx;
          margin-bottom: 16rpx;
          
          .current-price {
            font-size: 32rpx;
            font-weight: 600;
            color: #ff4d4f;
          }
          
          .original-price {
            font-size: 24rpx;
            color: #999;
            text-decoration: line-through;
          }
        }
        
        .progress-info {
          .progress-bar {
            width: 100%;
            height: 8rpx;
            background-color: #f0f0f0;
            border-radius: 4rpx;
            overflow: hidden;
            margin-bottom: 8rpx;
            
            .progress-fill {
              height: 100%;
              background: linear-gradient(90deg, #ff4d4f, #ff7875);
              border-radius: 4rpx;
              transition: width 0.3s ease;
            }
          }
          
          .progress-text {
            font-size: 24rpx;
            color: #999;
          }
        }
      }
    }
    
    .group-buy-btn {
      position: absolute;
      bottom: 30rpx;
      right: 30rpx;
      padding: 16rpx 32rpx;
      border-radius: 40rpx;
      font-size: 24rpx;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &.btn-group-buy {
        background-color: #ff4d4f;
        color: #fff;
        
        &:active {
          background-color: #d9363e;
        }
      }
      
      &.btn-upcoming {
        background-color: #faad14;
        color: #fff;
      }
      
      &.btn-ended {
        background-color: #d9d9d9;
        color: #666;
      }
      
      .btn-text {
        font-size: 24rpx;
      }
    }
  }
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
  
  .empty-text {
    margin-top: 20rpx;
    font-size: 28rpx;
    color: #999;
  }
}

.load-more {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40rpx 0;
  
  .load-text {
    font-size: 28rpx;
    color: #ff4d4f;
  }
}
</style>
